| Conditions | 13 |
| Paths | 120 |
| Total Lines | 122 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Vide.init often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 197 | Vide.prototype.init = function() { |
||
| 198 | var vide = this, |
||
| 199 | position = parsePosition(vide.settings.position), |
||
| 200 | sources, |
||
| 201 | poster; |
||
| 202 | |||
| 203 | // Set video wrapper styles |
||
| 204 | vide.$wrapper = $("<div>").css({ |
||
| 205 | "position": "absolute", |
||
| 206 | "z-index": -1, |
||
| 207 | "top": 0, |
||
| 208 | "left": 0, |
||
| 209 | "bottom": 0, |
||
| 210 | "right": 0, |
||
| 211 | "overflow": "hidden", |
||
| 212 | "-webkit-background-size": "cover", |
||
| 213 | "-moz-background-size": "cover", |
||
| 214 | "-o-background-size": "cover", |
||
| 215 | "background-size": "cover", |
||
| 216 | "background-repeat": "no-repeat", |
||
| 217 | "background-position": position.x + " " + position.y |
||
| 218 | }); |
||
| 219 | |||
| 220 | // Get poster path |
||
| 221 | poster = vide.path; |
||
| 222 | if (typeof vide.path === "object") { |
||
| 223 | if (vide.path.poster) { |
||
| 224 | poster = vide.path.poster; |
||
| 225 | } else { |
||
| 226 | if (vide.path.mp4) { |
||
| 227 | poster = vide.path.mp4; |
||
| 228 | } else if (vide.path.webm) { |
||
| 229 | poster = vide.path.webm; |
||
| 230 | } else if (vide.path.ogv) { |
||
| 231 | poster = vide.path.ogv; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | // Set video poster |
||
| 237 | // if (vide.settings.posterType === "detect") { |
||
| 238 | // findPoster(poster, function(url) { |
||
| 239 | // vide.$wrapper.css("background-image", "url(" + url + ")"); |
||
| 240 | // }); |
||
| 241 | // } else if (vide.settings.posterType !== "none") { |
||
| 242 | // vide.$wrapper |
||
| 243 | // .css("background-image", "url(" + poster + "." + vide.settings.posterType + ")"); |
||
| 244 | // } |
||
| 245 | |||
| 246 | // if parent element has a static position, make it relative |
||
| 247 | if (vide.$element.css("position") === "static") { |
||
| 248 | vide.$element.css("position", "relative"); |
||
| 249 | } |
||
| 250 | |||
| 251 | vide.$element.prepend(vide.$wrapper); |
||
| 252 | |||
| 253 | if (!isIOS && !isAndroid) { |
||
| 254 | sources = ""; |
||
| 255 | |||
| 256 | if (typeof vide.path === "object") { |
||
| 257 | if (vide.path.mp4) { |
||
| 258 | sources += "<source src='" + vide.path.mp4 + ".mp4' type='video/mp4'>"; |
||
| 259 | } |
||
| 260 | if (vide.path.webm) { |
||
| 261 | sources += "<source src='" + vide.path.webm + ".webm' type='video/webm'>"; |
||
| 262 | } |
||
| 263 | if (vide.path.ogv) { |
||
| 264 | sources += "<source src='" + vide.path.ogv + ".ogv' type='video/ogv'>"; |
||
| 265 | } |
||
| 266 | |||
| 267 | vide.$video = $("<video>" + sources + "</video>"); |
||
| 268 | } else { |
||
| 269 | vide.$video = $("<video>" + |
||
| 270 | "<source src='" + vide.path + ".mp4' type='video/mp4'>" + |
||
| 271 | "<source src='" + vide.path + ".webm' type='video/webm'>" + |
||
| 272 | "<source src='" + vide.path + ".ogv' type='video/ogg'>" + |
||
| 273 | "</video>"); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Disable visibility, while loading |
||
| 277 | vide.$video.css("visibility", "hidden"); |
||
| 278 | |||
| 279 | // Set video properties |
||
| 280 | vide.$video.prop({ |
||
| 281 | autoplay: vide.settings.autoplay, |
||
| 282 | loop: vide.settings.loop, |
||
| 283 | volume: vide.settings.volume, |
||
| 284 | muted: vide.settings.muted, |
||
| 285 | playbackRate: vide.settings.playbackRate |
||
| 286 | }); |
||
| 287 | |||
| 288 | // Append video |
||
| 289 | vide.$wrapper.append(vide.$video); |
||
| 290 | |||
| 291 | // Video alignment |
||
| 292 | vide.$video.css({ |
||
| 293 | "margin": "auto", |
||
| 294 | "position": "absolute", |
||
| 295 | "z-index": -1, |
||
| 296 | "top": position.y, |
||
| 297 | "left": position.x, |
||
| 298 | "-webkit-transform": "translate(-" + position.x + ", -" + position.y + ")", |
||
| 299 | "-ms-transform": "translate(-" + position.x + ", -" + position.y + ")", |
||
| 300 | "transform": "translate(-" + position.x + ", -" + position.y + ")" |
||
| 301 | }); |
||
| 302 | |||
| 303 | // resize video, when it's loaded |
||
| 304 | vide.$video.bind("loadedmetadata." + pluginName, function() { |
||
| 305 | vide.$video.css("visibility", "visible"); |
||
| 306 | vide.resize(); |
||
| 307 | vide.$wrapper.css("background-image", "none"); |
||
| 308 | }); |
||
| 309 | |||
| 310 | // resize event is available only for 'window', |
||
| 311 | // use another code solutions to detect DOM elements resizing |
||
| 312 | vide.$element.bind("resize." + pluginName, function() { |
||
| 313 | if (vide.settings.resizing) { |
||
| 314 | vide.resize(); |
||
| 315 | } |
||
| 316 | }); |
||
| 317 | } |
||
| 318 | }; |
||
| 319 | |||
| 443 |